/*
 * User_interface.c
 *
 * Created: 12/4/2013 9:53:28 AM
 *  Author: smhood
 */ 


#include <avr/io.h>
#include <stdlib.h>
#include <stdio.h>
#include "lcd.h"
#include "util.h"
#include <string.h>
#include "BAM.h"
#include "Scan_distance.h"
#include "final_movement.h"
#include "open_interface.h"
#include "sound.h"

char say[100];
oi_t *sensor_data;
int degree =0;
	
//List of all commands and initiallizes them at he beginning of the code.
void forward_move();

void backward_move();
	
void turn_around();

void turn_left();

void turn_right();

void forward_scan();

void full_scan();

void orientation();

void help();
	
	
int main(void)
{	
	//Resets and initializations for everything.
	lcd_clear();
	lcd_init();
	serial_init();
	sensor_data = oi_alloc(); 
	oi_init(sensor_data);
	
	//Sets the user key to n assigned user variable.
	char b = 'n';
	
	//Checks to see if the program is ready to be run by asking the user to click y. If any other key then y is selected then it doesn't begin. Once y is selected
	//plays sounds and begins program.
	while(b != 'y'){
		
		serial_puts("Are you ready to begin? If so Click 'y': \r\n");
	
		b = serial_getc();
		if(b == '\r'){
			serial_puts("\r\n");
		}
		else{
			serial_putc(b);
		}
		if(b != '\r') {
			
		}
		
		if(b == 'y'){
			//play sound
			serial_puts("\r\nLets begin your adventure at Iowa State!. \r\n");
			sound();
			break;
		}
		else{
			serial_puts("\r\nYou need to press 'y' to begin. \r\n");
		}
	}	
	
	//Takes in User commands and sends them to functions.
	char cmd = 0;
	//Sets a bunch of assigned user commands for the user to use to control the robot, if none of the commands listed are put it keeps asking for the a proper command.
	while(cmd != 'p'){
		
		strcpy(say, "Whats your command?\r\n");
		serial_puts(say);
		
		b = serial_getc();
		if(b == '\r'){
			serial_puts("\r\n");
		}
		else{
			serial_putc(b);
		}
		if(b != '\r') {
			cmd = b;
		}
			
		//calls the movement functions
		if(cmd == 'w'){
			forward_move();
		}
		//calls backward movement functions
		else if(cmd == 'x'){
			backward_move();
		}
		//turns around completely
		else if(cmd == 's'){
			turn_around();
		}
		//turns left
		else if(cmd == 'a'){
			turn_left();
		}
		//turns right
		else if(cmd == 'd'){
			turn_right();
		}
		//calls the short scan function in Scan_Distance.c
		else if(cmd == 'q'){
			forward_scan();
		}
		//calls the full scan function in Scan_Distance.
		else if(cmd == 'e'){
			full_scan();
		}
		//Calls a function that keeps track of starting angle and direction.
		else if(cmd == 'o'){
			orientation();
		}
		//Gives list of all commands to user.
		else if(cmd == 'h'){
			help();
		}
		//Kills the program and plays the sound for finishing.
		else if(cmd == 'p'){
			serial_puts("\r\nHoly crap you passed CPRE 281!! Or you failed miserably...\r\n");
			lprintf("Did I Win!?!?!");
			sound();
		}
		else{
			serial_puts("\r\nPress one of the assigned commands, press 'h' for command list.\r\n");
		}
		
	}
}

void forward_move(){
	lcd_clear();
	
	
	serial_puts("\r\nHow far do you want to move? (Input an integer)\r\n"); // Or make it set movement 
	
	//Askes user for number, records it and waits for user to press enter for it to record value and set it to the variable move.
	char buffer[100] = {};
	int i = 0;
	char b = 0;
	while(b != '\r'){
		b = serial_getc();
		if(b == '\r'){
			serial_puts("\r\n");
		}
		else{
			serial_putc(b);
		}
		if(b != '\r') {
			buffer[i] = b;
			i++;
		}
	}	
	

	//Takes string of numbers and turns it into an int variable to be given too the move function.
	int move_forward = atoi(buffer);
	lprintf("Moving %d !", move_forward);
	//Send a call to forward movement NOT YET IMPLEMENTED
	
	forward(move_forward);
	
}


void backward_move(){
	lcd_clear();
	
	
	serial_puts("\r\nHow far do you want to move? (Input an integer)\r\n"); // Or make it set movement 
	
	//Askes user for number, records it and waits for user to press enter for it to record value and set it to the variable move.
	char buffer[100] = {};
	int i = 0;
	char b = 0;
	while(b != '\r'){
		b = serial_getc();
		if(b == '\r'){
			serial_puts("\r\n");
		}
		else{
			serial_putc(b);
		}
		if(b != '\r') {
			buffer[i] = b;
			i++;
		}
	}	
	
	//Takes string of numbers and turns it into an int variable to be given too the move function.
	int move_back = atoi(buffer);
	lprintf("Moving %d !", move_back);
	backward(move_back);
	}
	
void turn_around(){
	lcd_clear();
	
	//Calls to the turn right function to turn around completely.
	right_turn(180);
	serial_puts("\r\n180!\n\r");
	
	lprintf("Weeeeee!");
	//Adds 180 defrees to the the direction tracker.
	degree += 180;
}

void turn_left(){
	lcd_clear();
	
	serial_puts("\r\nHow far should I turn? (Input an integer 1 - 90\r\n");
	//getc that collects the integer between 1 - 90
	//Askes user for number, records it and waits for user to press enter for it to record value and set it to the variable move.
	char buffer[100] = {};
	int i = 0;
	char b = 0;
	while(b != '\r'){
		b = serial_getc();
		if(b == '\r'){
			serial_puts("\r\n");
		}
		else{
			serial_putc(b);
		}
		if(b != '\r') {
			buffer[i] = b;
			i++;
		}
	}
	//Takes string of numbers and turns it into an int variable to be given too the turn the robot using the function.
	int left = atoi(buffer);
	lprintf("Turning Right %d Degrees!", left);
	//subtracts the amount of the given turn to the direction of the robots current direction
	degree -= left;
	//If the number is not between 0 and 90 then it will re call the function and ask for the proper number. 
	if(left > 0 && left <= 90){
		serial_puts("Turning Left.\r\n");
		//turn left said degrees.
		left_turn(left);
	}
	else{
		serial_puts("Thats not a proper degree.\r\n");
		turn_left();
	}		
	
}

void turn_right(){
	lcd_clear();
	
	serial_puts("\r\nHow far should I turn? (Input an integer 1 - 90\r\n");
	//getc that collects the integer between 1 - 90
	//Askes user for number, records it and waits for user to press enter for it to record value and set it to the variable move.
	char buffer[100] = {};
	int i = 0;
	char b = 0;
	while(b != '\r'){
		b = serial_getc();
		if(b == '\r'){
			serial_puts("\r\n");
		}
		else{
			serial_putc(b);
		}
		if(b != '\r') {
			buffer[i] = b;
			i++;
		}
	}
	int right = atoi(buffer);
	lprintf("Turning Right %d Degrees!", right);
	//Adds to the initial direction of the robot in order to track its current direction from initial placement.
	degree += right;
	//Takes string of numbers and turns it into an int variable to be given too the turn the robot using the function, if the number given isnt between 0 and 90 then calls for a new number.
	if(right > 0 && right <= 90){
		serial_puts("Turning Right.\r\n");
		//turn right said degrees.
		right_turn(right);
	}
	else{
		serial_puts("Thats not a proper degree.\r\n");
		turn_right();
	}
	
}
//Calls to scan_front() in Scan_Distance.c
void forward_scan(){
	scan_front();
}
//Calls full scan from the Scan_Distance.c
void full_scan(){
	scan();
}
//Prints to the user what is the current direction based off the initial direction of the robot.
void orientation(){
	
	int i = (degree + 720)%360;// adds 720 to the degree to account for movement to the left and does a percent mod to keep in the range of 0-359
	
	char buf[10];
	sprintf(buf, "\r\n%d\r\n", i);
	serial_puts(buf);
}
//Prints to the user a list of commands that the robot will accept.
void help(){
	lcd_clear();
	
	serial_puts("\r\nForward = w\r\n");
	
	serial_puts("Backward = x\r\n");
	
	serial_puts("Turn around = s\r\n");
	
	serial_puts("Turn left = a\r\n");
	
	serial_puts("Turn right = d\r\n");
	
	serial_puts("Forward Scan = q\r\n");
	
	serial_puts("Full Scan = e\r\n");
	
	serial_puts("orientation = o\r\n");
	
	lprintf("Help!?!?");
}